package com.yinhai.message.manage.monitor.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

import javax.annotation.Resource;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yinhai.core.app.ta3.web.controller.BaseController;


/**
 * 队列监听
 * @author yanglq 2017.3.23
 *
 */
@RequestMapping("messageMonitor")
@Controller
public class MessageMonitorController extends BaseController{

	@Resource(name = "threadPoolTaskExecutor")
	private ThreadPoolTaskExecutor executor;

	/**
	 * 页面初始化
	 * @return
	 */
	@RequestMapping("messageMonitorController.do")
	public String execute(){
		setList("queueGrid", getQueueState());
		return "/manage/monitor/queueMonitor";
	}

	/**
	 * 刷新
	 * @return
	 */
	@RequestMapping("messageMonitorController!show.do")
	public String toShow(){
		setList("queueGrid", getQueueState());
		return JSON;
	}

	private List<Map<String,String>> getQueueState(){
		List<Map<String,String>> list = new ArrayList<>();
		Map<String,String> map = new HashMap<>();
		map.put("size", "1000");
		ThreadPoolExecutor ex = executor.getThreadPoolExecutor();
		map.put("currentSize", String.valueOf(ex.getQueue().size()));
		map.put("completeTask", String.valueOf(ex.getCompletedTaskCount()));
		map.put("totalTask", String.valueOf(ex.getTaskCount()));
		map.put("activeCount", String.valueOf(ex.getActiveCount()));
		map.put("currentPoolSize", String.valueOf(ex.getPoolSize()));
		map.put("corePoolSize", String.valueOf(ex.getCorePoolSize()));
		map.put("largestPoolSize", String.valueOf(ex.getLargestPoolSize()));
		list.add(map);
		return list;
	}
	
}
